var req = new XMLHttpRequest();

function login() {
	// Store the values
	var login_name = document.getElementById("login_name").value;
	var login_pass = document.getElementById("login_pass").value;
	var connection_string = "https://" + login_name + ":" + login_pass
			+ "@mobilevikings.com/api/1.0/rest/mobilevikings/sim_balance.xml";
	encodeURIComponent(connection_string);

	req.onreadystatechange = readCredit;
	try {
		req.open('GET', connection_string);
		req.send(null);
	} catch (e) {
		alert("An exception occurred in the script. Error name: " + e.name
				+ ". Error message: " + e.message);
	}
}

function readCredit() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			document.getElementById("credit").style.display = "block";
			document.getElementById("login").style.display = "none";

			var xmldoc = req.responseXML;
			var root = xmldoc.getElementsByTagName('dict').item(0);
			var node = root.childNodes.item(0);
			var sim_credit = xmldoc.getElementsByTagName("credits")[0].childNodes[0].nodeValue;
			var sim_sms = xmldoc.getElementsByTagName("sms")[0].childNodes[0].nodeValue;
			var sim_data = xmldoc.getElementsByTagName("data")[0].childNodes[0].nodeValue;
			var sim_expired = xmldoc.getElementsByTagName("is_expired")[0].childNodes[0].nodeValue;
			var sim_valid_until = xmldoc.getElementsByTagName("valid_until")[0].childNodes[0].nodeValue;

			document.getElementById("sim_credit").innerHTML = "Credit: "
					+ sim_credit;
			document.getElementById("sim_countdown").innerHTML = sim_expired;
			if (sim_expired == "False") {
				document.getElementById("sim_sms").innerHTML = "SMS: "
						+ sim_sms;
				document.getElementById("sim_data").innerHTML = "Data: "
						+ sim_data;
				var daysLeft = 3;
				if (daysLeft == 0) {
					document.getElementById("sim_countdown").innerHTML = "Your free data and SMSes will expire today.";
				} else {
					if (daysLeft == 1) {
						document.getElementById("sim_countdown").innerHTML = "Your free data and SMSes will expire tomorrow.";
					} else {
						document.getElementById("sim_countdown").innerHTML = "Your free data and SMSes will expire in "
								+ daysLeft + " days.";
					}
				}

			} else {
				document.getElementById("sim_expired").innerHTML = "Your free data and SMSes have expired.";
			}
			document.getElementById("debug").innerHTML = req.status + " | "
					+ req.readyState;
			document.getElementById("credit").style.display = "block";
			document.getElementById("login").style.display = "none";
		}
	}
}

function getDaysLeft(date) {
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();

	arr_date1 = date.split("T");
	var temp = arr_date1[0];
	arr_date2 = temp.split("-");

	var days_exp = arr_date2[2];
	var months_exp = arr_date2[1];
	var years = arr_date2[0];

	var return_days = 0;

	if (months_exp == month) {
		return_days = days_exp - day;
	} else {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month || 10 || month == 12) {
			return_days = 31 - day + days_exp;
		} else {
			if (month == 2) {
				if (checkleapyear(years)) {
					return_days = 29 - day + days_exp;
				} else {
					return_days = 28 - day + days_exp;
				}
			} else {
				return_days = 30 - day + days_exp;
			}
		}
	}

	return return_days;
}

function checkleapyear(datea) {
	datea = parseInt(datea);
	if (datea % 4 == 0) {
		if (datea % 100 != 0) {
			return true;
		} else {
			if (datea % 400 == 0)
				return true;
			else
				return false;
		}
	}
	return false;
}
